package com.example.onlinemusic.controller;

import com.example.onlinemusic.mapper.LoveMusicMapper;
import com.example.onlinemusic.model.Contant;
import com.example.onlinemusic.model.LoveMusic;
import com.example.onlinemusic.model.Music;
import com.example.onlinemusic.model.User;
import com.example.onlinemusic.tools.ResponseBodyMessage;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: hold on
 * Date: 2022-08-01
 * Time: 20:25
 */
@RestController
@RequestMapping("/lovemusic")
public class LoveMusicController {

    //注入LoveMusicMapper
    @Resource
    private LoveMusicMapper loveMusicMapper;

    //收藏音乐
    @RequestMapping("/likeMusic")
    public ResponseBodyMessage<Boolean> insertLoveMusic(@RequestParam Integer id,HttpServletRequest request){
        //1.检查登入状态,未登入不创建回话
        HttpSession session = request.getSession(false);
        if(session==null||null==request.getSession().getAttribute(Contant.USERINFO_SESSION_KEY)){
           //未登入状态
           return new ResponseBodyMessage<>(-1,"请登入用户",false);
        }
        //登入状态,进行音乐收藏
        //1.获取到用户id
        User user = (User)session.getAttribute(Contant.USERINFO_SESSION_KEY);
        int user_id = user.getId();
        System.out.println("user_id:"+user_id+" music_id:"+id);
        //2.查询该歌曲是否已存在收藏列表
        LoveMusic loveMusic = loveMusicMapper.findLoveMusicByUidAndMusicId(user_id,id);
        if(loveMusic!=null){
            //该歌曲已收藏!
            System.out.println("lovemusic:"+loveMusic);
            return new ResponseBodyMessage<>(-1,"收藏失败,该歌曲收藏",false);
        }
        //未收藏,将其收藏!
        int flg = loveMusicMapper.insetLoveMusic(user_id,id);
        if(flg!=1){
            return new ResponseBodyMessage<>(-1,"收藏失败",false);
        }
        return new ResponseBodyMessage<>(0,"收藏成功!",true);
    }

    //通过音乐名称查询收藏列表
    @RequestMapping("/findloveMusic")
    public ResponseBodyMessage<List<Music>>
    findLoveMusicByUidAndMusicName(@RequestParam(required = false) String musicName,
                                   HttpServletRequest request){
        //1.检查登入状态,未登入不创建回话
        HttpSession session = request.getSession(false);
        if(session==null||null==request.getSession().getAttribute(Contant.USERINFO_SESSION_KEY)){
            //未登入状态
            return new ResponseBodyMessage<>(-1,"请登入用户",null);
        }
        //登入状态,进行音乐查询
        //1.获取到用户id
        User user = (User)session.getAttribute(Contant.USERINFO_SESSION_KEY);
        int user_id = user.getId();
        System.out.println("user_id:"+user_id+" musicName:"+musicName);
        //2.查询收藏列表
        List<Music> musicList = null;
        if(musicName==null){
            //2.1歌曲名称为空,查询该用户所有收藏歌曲!
           musicList = loveMusicMapper.findLoveMusic(user_id);
           return new ResponseBodyMessage<>(0,"查询到收藏列表",musicList);
        }
        //2.2 歌曲名称不为空,模糊查询
        musicList = loveMusicMapper.findLoveMusicByUidAndMusicName(user_id,musicName);
        return new ResponseBodyMessage<>(0,"查询收藏列表成功",musicList);
    }

    //通过收藏表中的id取消收藏歌曲信息
    @RequestMapping("/deleteloveMusic")
    public ResponseBodyMessage<Boolean> deleteLoveMusic(@RequestParam Integer id,HttpServletRequest request){
        //1.检查登入状态,未登入不创建回话
        HttpSession session = request.getSession(false);
        if(session==null||null==request.getSession().getAttribute(Contant.USERINFO_SESSION_KEY)){
            //未登入状态
            return new ResponseBodyMessage<>(-1,"请登入用户",false);
        }
        //登入状态,可进行取消音乐收藏功能
        //1.获取到用户id
        User user = (User)session.getAttribute(Contant.USERINFO_SESSION_KEY);
        int user_id = user.getId();
        //取消收藏!
        int ret = loveMusicMapper.deleteLoveMusicByUidAndMusicId(user_id,id);
        if(ret!=1){
            return new ResponseBodyMessage<>(-1,"取消收藏失败",false);
        }
        return new ResponseBodyMessage<>(0,"取消收藏成功",true);
    }
}
